home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / LBITOPS.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  1KB  |  47 lines

  1. /*
  2. **  large bit array operations by Scott Dudley
  3. **  with modifications by Auke Reitsma and Bob Stout
  4. **
  5. **  Public domain
  6. */
  7.  
  8. #include <limits.h>
  9.  
  10. /*
  11. **  The following macros assume CHAR_BIT is one of either 8, 16, or 32
  12. */
  13.  
  14. #define MASK  CHAR_BIT-1
  15. #define SHIFT ((CHAR_BIT==8)?3:(CHAR_BIT==16)?4:8)
  16.  
  17. #define BitOff(a,x)  ((void)((a)[(x)>>SHIFT] &= ~(1 << ((x)&MASK))))
  18. #define BitOn(a,x)   ((void)((a)[(x)>>SHIFT] |=  (1 << ((x)&MASK))))
  19. #define BitFlip(a,x) ((void)((a)[(x)>>SHIFT] ^=  (1 << ((x)&MASK))))
  20. #define IsBit(a,x)   ((a)[(x)>>SHIFT]        &   (1 << ((x)&MASK)))
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24.  
  25. int main(void)
  26. {
  27.       char array[64];
  28.  
  29.       memset(array, '\0', sizeof(array));
  30.  
  31.       BitOn(array, 5);
  32.       BitOn(array, 12);
  33.       BitOn(array, 500);
  34.  
  35.       if (IsBit(array, 5) && IsBit(array, 12) && IsBit(array, 500))
  36.             puts("These functions seem to work!");
  37.       else  puts("Something's broken here!");
  38.  
  39.       BitFlip(array, 12);
  40.       BitOff(array, 5);
  41.  
  42.       if (!IsBit(array, 5) && !IsBit(array, 12) && IsBit(array, 500))
  43.             puts("These functions still seem to work!");
  44.       else  puts("Something's broken here!");
  45.       return 0;
  46. }
  47.